﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Common;
using Griffin.MvcContrib.Localization.Types;
using Griffin.MvcContrib.Localization.Views;

namespace AutoPartsSearch.Web.Infrastructure.Cache.Localizations
{
    public class CachedViewLocalizer : ViewLocalizer
    {
        private readonly int _timeToHoldCache = 5;

        public CachedViewLocalizer(IViewLocalizationRepository repository, IConfigurationGetter configurationGetter)
            : base(repository)
        {
            int value;
            if (int.TryParse(configurationGetter.GetConfigurationValue(Constants.LocalizationCacheInMinutes), out value))
            {
                this._timeToHoldCache = value;
            }
        }

        public override string Translate(RouteData routeData, string text)
        {
            return CacheExecutor.Execute(() => base.Translate(routeData, text), _timeToHoldCache, routeData, text);
        }

        public override string Translate(string viewPath, RouteData routeData, string text)
        {
            return CacheExecutor.Execute(() => base.Translate(viewPath, routeData, text), _timeToHoldCache, viewPath, routeData, text);
        }
    }
}